package com.fpno2.JDBCUtil;

import org.junit.Test;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtils {
    //连接
    public static Connection getConnection() throws Exception{
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);

        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

        Class.forName(driverClass);

        Connection con = DriverManager.getConnection(url, user, password);
        //System.out.println(con);

        return con;
    }

    //关闭
    public static void closeResource(Connection con, Statement st)  {
        try {
            if (st!=null){
            st.close();}
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            if (con!=null){
            con.close();}
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
